/**
 * Copyright &copy; 2012-2015 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.soccer.cms.modules.gen.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.soccer.cms.common.config.Global;
import com.soccer.cms.common.persistence.Page;
import com.soccer.cms.common.web.BaseController;
import com.soccer.cms.common.utils.StringUtils;
import com.soccer.cms.modules.gen.entity.GenReferentity;
import com.soccer.cms.modules.gen.service.GenReferentityService;

/**
 * 关联实体Controller
 * @author zouqh
 * @version 2015-05-25
 */
@SuppressWarnings("unused")
@Controller
@RequestMapping(value = "${adminPath}/gen/genReferentity")
public class GenReferentityController extends BaseController {

	@Autowired
	private GenReferentityService genReferentityService;
	
	@ModelAttribute
	public GenReferentity get(@RequestParam(required=false) String id) {
		GenReferentity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = genReferentityService.get(id);
		}
		if (entity == null){
			entity = new GenReferentity();
		}
		return entity;
	}
	
	@RequiresPermissions("gen:genReferentity:view")
	@RequestMapping(value = {"index"})
	public String indexPage(GenReferentity genReferentity, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "modules/gen/genReferentityIndex";
	}
	
	@RequiresPermissions("gen:genReferentity:view")
	@RequestMapping(value = {"list", ""})
	public String list(GenReferentity genReferentity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GenReferentity> page = genReferentityService.findPage(new Page<GenReferentity>(request, response), genReferentity); 
		model.addAttribute("page", page);
		return "modules/gen/genReferentityList";
	}

	@RequiresPermissions("gen:genReferentity:view")
	@RequestMapping(value = "form")
	public String form(GenReferentity genReferentity, Model model) {
		model.addAttribute("genReferentity", genReferentity);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt",request.getParameter("opt"));
		return "modules/gen/genReferentityForm";
	}

	@RequiresPermissions("gen:genReferentity:edit")
	@RequestMapping(value = "save")
	public String save(GenReferentity genReferentity, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, genReferentity)){
			return form(genReferentity, model);
		}
		genReferentityService.save(genReferentity);
		//addMessage(redirectAttributes, "保存关联实体成功");
		//return "redirect:"+Global.getAdminPath()+"/gen/genReferentity/?repage";
		model.addAttribute("genReferentity", genReferentity);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt","save");
		model.addAttribute("message","保存关联实体成功");
		return "modules/gen/genReferentityForm";
	}
	
	@RequiresPermissions("gen:genReferentity:edit")
	@RequestMapping(value = "delete")
	public String delete(GenReferentity genReferentity, RedirectAttributes redirectAttributes) {
		genReferentityService.delete(genReferentity);
		addMessage(redirectAttributes, "删除关联实体成功");
		return "redirect:"+Global.getAdminPath()+"/gen/genReferentity/?repage";
	}

	@RequiresPermissions("gen:genReferentity:edit")
	@RequestMapping(value = "batchDelete")
	public String delete(String ids, RedirectAttributes redirectAttributes) {
		genReferentityService.delete(ids);
		addMessage(redirectAttributes, "删除关联实体成功");
		return "redirect:"+Global.getAdminPath()+"/gen/genReferentity/?repage";
	}
}